<?php


namespace app\printing\model;


use think\Db;
use think\Model;

class PrintingModel extends Model 
{
    public $pk = 'printing_id';
    public $table = 'dev_printing';

    public function __construct($data = [])
    {
        parent::__construct($data);
        header("Content-type:text/html;charset=UTF-8");
    }
    public function PrintingOne($printing_id){
        $UserID = PrintingModel::field('user_id')->find($printing_id);
        return $UserID['user_id'];
    }
    //我的印刷单子
    public function MyPrintingList($current_user,$where){
        //指定状态排序
        $paging = paging();
        $contract_types = "2,3,4,5,1";

        $exp = new \think\Db\Expression('field(status,'.$contract_types.'),create_time DESC');
        $count = PrintingModel::where('user_id',$current_user)
            ->where('is_del',1)
            ->where($where)
            ->order($exp)
            ->count();
        $StartEnd = showPage($count,$paging['page'],$paging['limit']);
        $PrintingList = PrintingModel::where('user_id',$current_user)
            ->where('is_del',1)
            ->where($where)
            ->order($exp)
            ->limit($StartEnd['start'],$StartEnd['end'])
            ->select()->toArray();
       // dump(PrintingModel::getLastSql());exit;
        foreach($PrintingList as $key=>$val){

            $PrintingList[$key]['customer_name'] = iconv('gbk','utf-8', $val['customer_name']);
            $PrintingList[$key]['order_number'] = iconv('gbk','utf-8', $val['order_number']);
            $PrintingList[$key]['remarks'] = iconv('gbk','utf-8', $val['remarks']);
            $PrintingList[$key]['currency'] = iconv('gbk','utf-8', $val['currency']);
            if ($val['purchase_content']){
                $PrintingList[$key]['purchase_content'] = iconv('gbk','utf-8', $val['purchase_content']);
            }
            if ($val['inspector_content']){
                $PrintingList[$key]['inspector_content'] = iconv('gbk','utf-8', $val['inspector_content']);
            }
            if ($val['reject_content']){
                $PrintingList[$key]['reject_content'] = iconv('gbk','utf-8', $val['reject_content']);
            }


            $PrintingList[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $UserDetail = Db::table('user')->field('user_name')->find($val['user_id']);
            $PrintingList[$key]['user_name'] = iconv('gbk','utf-8', $UserDetail['user_name']);
            $PrintingList[$key]['tips'] = $this->printing_status($val['status']);
        }
        return ['data'=>$PrintingList,'count'=>$count];
    }
    public function SalesPrintingList($where){
        $paging = paging();
        $contract_types = "2,3,4,5,1";
        $exp = new \think\Db\Expression('field(status,'.$contract_types.'),create_time DESC');
        $count = PrintingModel::
            where('is_del',1)
            ->where($where)
            ->order($exp)
            ->count();
        $StartEnd = showPage($count,$paging['page'],$paging['limit']);
        $PrintingList = PrintingModel::
            where('is_del',1)
            ->where($where)
            ->order($exp)
            ->limit($StartEnd['start'],$StartEnd['end'])
            ->select()->toArray();

        foreach($PrintingList as $key=>$val){

            $PrintingList[$key]['customer_name'] = iconv('gbk','utf-8', $val['customer_name']);
            $PrintingList[$key]['order_number'] = iconv('gbk','utf-8', $val['order_number']);
            $PrintingList[$key]['remarks'] = iconv('gbk','utf-8', $val['remarks']);
            $PrintingList[$key]['currency'] = iconv('gbk','utf-8', $val['currency']);
            if ($val['purchase_content']){
                $PrintingList[$key]['purchase_content'] = iconv('gbk','utf-8', $val['purchase_content']);
            }
            if ($val['inspector_content']){
                $PrintingList[$key]['inspector_content'] = iconv('gbk','utf-8', $val['inspector_content']);
            }
            if ($val['reject_content']){
                $PrintingList[$key]['reject_content'] = iconv('gbk','utf-8', $val['reject_content']);
            }

            $PrintingList[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $UserDetail = Db::table('user')->field('user_name')->find($val['user_id']);
            $PrintingList[$key]['user_name'] = iconv('gbk','utf-8', $UserDetail['user_name']);
            $PrintingList[$key]['tips'] = $this->printing_status($val['status']);
        }
        return ['data'=>$PrintingList,'count'=>$count];
    }


    //采购部印刷单的 两种状态
    public function PurchasePrintingList($status){
        $where = [];
        if ($status == 1){
            $where['purchase_status'] = ['in',[1,3]];
            $where['status'] = ['in',[1,3,4,5]];
        }else if ($status == 2){
            $where['purchase_status'] = 2;
            $where['status'] = 2;
        }
        $paging = paging();
        $contract_types = "2,3,4,5,1";
        $exp = new \think\Db\Expression('field(status,'.$contract_types.'),create_time DESC');
        $count = PrintingModel::
                where($where) //审批状态
            ->where('is_del',1)
            ->order($exp)
            ->count();

        $StartEnd = showPage($count,$paging['page'],$paging['limit']);
        $PrintingList = PrintingModel::
            where($where) //审批状态
            ->where('is_del',1)
            ->order($exp)
            ->limit($StartEnd['start'],$StartEnd['end'])
            ->select()->toArray();

        foreach($PrintingList as $key=>$val){
            $PrintingList[$key]['customer_name'] = iconv('gbk','utf-8', $val['customer_name']);
            $PrintingList[$key]['order_number'] = iconv('gbk','utf-8', $val['order_number']);
            $PrintingList[$key]['remarks'] = iconv('gbk','utf-8', $val['remarks']);
            $PrintingList[$key]['currency'] = iconv('gbk','utf-8', $val['currency']);
            if ($val['purchase_content']){
                $PrintingList[$key]['purchase_content'] = iconv('gbk','utf-8', $val['purchase_content']);
            }
            if ($val['inspector_content']){
                $PrintingList[$key]['inspector_content'] = iconv('gbk','utf-8', $val['inspector_content']);
            }
            //驳回原因
            if ($val['reject_content']){
                $PrintingList[$key]['reject_content'] = iconv('gbk','utf-8', $val['reject_content']);
            }
            $PrintingList[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $UserDetail = Db::table('user')->field('user_name')->find($val['user_id']);
            $PrintingList[$key]['user_name'] = iconv('gbk','utf-8', $UserDetail['user_name']);
            $PrintingList[$key]['tips'] = $this->printing_status($val['status']);
        }
        return ['data'=>$PrintingList,'count'=>$count];
    }

    public function SearchPurchasePrintingList($where){
        if ($where['status'] == 1){
            $where['purchase_status'] = ['in',[1,3]];
            $where['status'] = ['in',[1,3,4,5]];
        }else if ($where['status'] == 2){
            $where['purchase_status'] = 2;
            $where['status'] = 2;
        }else{
            //全部
            $where['purchase_status'] = ['in',[1,2,3]];
            $where['status'] = ['in',[1,2,3,4,5]];
        }
        $paging = paging();
        $contract_types = "2,3,4,5,1";
        $exp = new \think\Db\Expression('field(status,'.$contract_types.'),create_time DESC');
        $count = PrintingModel::
        where($where) //审批状态
        ->where('is_del',1)
            ->order($exp)
            ->count();

        $StartEnd = showPage($count,$paging['page'],$paging['limit']);
        $PrintingList = PrintingModel::
        where($where) //审批状态
        ->where('is_del',1)
            ->order($exp)
            ->limit($StartEnd['start'],$StartEnd['end'])
            ->select()->toArray();

        foreach($PrintingList as $key=>$val){
            $PrintingList[$key]['customer_name'] = iconv('gbk','utf-8', $val['customer_name']);
            $PrintingList[$key]['order_number'] = iconv('gbk','utf-8', $val['order_number']);
            $PrintingList[$key]['remarks'] = iconv('gbk','utf-8', $val['remarks']);
            $PrintingList[$key]['currency'] = iconv('gbk','utf-8', $val['currency']);
            if ($val['purchase_content']){
                $PrintingList[$key]['purchase_content'] = iconv('gbk','utf-8', $val['purchase_content']);
            }
            if ($val['inspector_content']){
                $PrintingList[$key]['inspector_content'] = iconv('gbk','utf-8', $val['inspector_content']);
            }
            //驳回原因
            if ($val['reject_content']){
                $PrintingList[$key]['reject_content'] = iconv('gbk','utf-8', $val['reject_content']);
            }
            $PrintingList[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $UserDetail = Db::table('user')->field('user_name')->find($val['user_id']);
            $PrintingList[$key]['user_name'] = iconv('gbk','utf-8', $UserDetail['user_name']);
            $PrintingList[$key]['tips'] = $this->printing_status($val['status']);
        }
        return ['data'=>$PrintingList,'count'=>$count];
    }




    //总经办印刷单的 两种状态
    public function GeneralPrintingList($status){
        $where = [];
        if ($status == 1){
            $where['general_status'] = ['in',[1,3]];
            $where['status'] = ['in',[1,4,5]];
        }else if ($status == 2){
            $where['general_status'] = 2;
            $where['status'] = 3;
        }
        $paging = paging();
        $contract_types = "2,3,4,5,1";
        $exp = new \think\Db\Expression('field(status,'.$contract_types.'),create_time DESC');
        $count = PrintingModel::
        where($where) //审批状态
        ->where('is_del',1)
            ->order($exp)
            ->count();

        $StartEnd = showPage($count,$paging['page'],$paging['limit']);
        $PrintingList = PrintingModel::
        where($where) //审批状态
        ->where('is_del',1)
            ->order($exp)
            ->limit($StartEnd['start'],$StartEnd['end'])
            ->select()->toArray();

        foreach($PrintingList as $key=>$val){
            $PrintingList[$key]['customer_name'] = iconv('gbk','utf-8', $val['customer_name']);
            $PrintingList[$key]['order_number'] = iconv('gbk','utf-8', $val['order_number']);
            $PrintingList[$key]['remarks'] = iconv('gbk','utf-8', $val['remarks']);
            $PrintingList[$key]['currency'] = iconv('gbk','utf-8', $val['currency']);
            if ($val['purchase_content']){
                $PrintingList[$key]['purchase_content'] = iconv('gbk','utf-8', $val['purchase_content']);
            }
            if ($val['inspector_content']){
                $PrintingList[$key]['inspector_content'] = iconv('gbk','utf-8', $val['inspector_content']);
            }
            if ($val['reject_content']){
                $PrintingList[$key]['reject_content'] = iconv('gbk','utf-8', $val['reject_content']);
            }

            $PrintingList[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $UserDetail = Db::table('user')->field('user_name')->find($val['user_id']);
            $PrintingList[$key]['user_name'] = iconv('gbk','utf-8', $UserDetail['user_name']);
            $PrintingList[$key]['tips'] = $this->printing_status($val['status']);
        }
        return ['data'=>$PrintingList,'count'=>$count];
    }

    //质检部印刷单的 两种状态
    public function InspectorPrintingList($status){
        $where = [];
        if ($status == 1){
            $where['inspector_status'] = ['in',[1,3]];
            $where['status'] = ['in',[1,4,5]];
        }else if ($status == 2){
            $where['inspector_status'] = 2;
            $where['status'] = 4;
        }
        $paging = paging();
        $contract_types = "2,3,4,5,1";
        $exp = new \think\Db\Expression('field(status,'.$contract_types.'),create_time DESC');
        $count = PrintingModel::
        where($where) //审批状态
        ->where('is_del',1)
            ->order($exp)
            ->count();

        $StartEnd = showPage($count,$paging['page'],$paging['limit']);
        $PrintingList = PrintingModel::
        where($where) //审批状态
        ->where('is_del',1)
            ->order($exp)
            ->limit($StartEnd['start'],$StartEnd['end'])
            ->select()->toArray();

        foreach($PrintingList as $key=>$val){
            $PrintingList[$key]['customer_name'] = iconv('gbk','utf-8', $val['customer_name']);
            $PrintingList[$key]['order_number'] = iconv('gbk','utf-8', $val['order_number']);
            $PrintingList[$key]['remarks'] = iconv('gbk','utf-8', $val['remarks']);
            $PrintingList[$key]['currency'] = iconv('gbk','utf-8', $val['currency']);
            if ($val['purchase_content']){
                $PrintingList[$key]['purchase_content'] = iconv('gbk','utf-8', $val['purchase_content']);
            }
            if ($val['inspector_content']){
                $PrintingList[$key]['inspector_content'] = iconv('gbk','utf-8', $val['inspector_content']);
            }
            if ($val['reject_content']){
                $PrintingList[$key]['reject_content'] = iconv('gbk','utf-8', $val['reject_content']);
            }
            $PrintingList[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $UserDetail = Db::table('user')->field('user_name')->find($val['user_id']);
            $PrintingList[$key]['user_name'] = iconv('gbk','utf-8', $UserDetail['user_name']);
            $PrintingList[$key]['tips'] = $this->printing_status($val['status']);
        }
        return ['data'=>$PrintingList,'count'=>$count];
    }
    //总单状态
    public function printing_status($status){

        switch ($status){
            case $status == 1:
                $tips = '<span style="color: green">已完成</span>';
                break;
            case $status == 2:
                $tips = '<span style="color: yellowgreen">采购部审批中</span>';
                break;
            case $status == 3:
                $tips = '<span style="color: yellowgreen">总经办审批中</span>';
                break;
            case $status == 4:
                $tips = '<span style="color: yellowgreen">质检部审批中</span>';
                break;
            case $status == 5:
                $tips = '<span style="color: red">未通过</span>';
                break;
            default :
                $tips = 'notfound';
        }
        return $tips;
    }
    //印刷单下的商品
    public function PrintingByArticles($printing_id){

        $paging = paging();
        $count  = ArticlesModel::
             where('printing_id',$printing_id)
            ->count();
        $start_end = showPage($count,$paging['page'],$paging['limit']);
        $List = ArticlesModel::
            where('printing_id',$printing_id)
            ->limit($start_end['start'],$start_end['end'])
            ->select()
            ->toArray();
        foreach($List as $key=>$val){
            $List[$key]['articles_content'] = iconv('gbk','utf-8',$val['articles_content']);
            if ($val['articles_edition']){
                $List[$key]['articles_edition'] = iconv('gbk','utf-8',$val['articles_edition']);
            }
            $List[$key]['name'] = iconv('gbk','utf-8',$val['name']);
            $List[$key]['value'] = iconv('gbk','utf-8',$val['value']);
            $List[$key]['special'] = iconv('gbk','utf-8',$val['special']);
            if ($val['user_file']){
                $List[$key]['user_file'] = '<a style="color: white;padding: 6px 10px 6px 10px;border-radius: 15%;background-color: black;" href='.$val['user_file'].' target="_blank">点击查看</a></div>';
            }
        }
        return ['data'=>$List,'count'=>$count];
    }
    //采购部审批 并提交内容
    public function PurchaseApprove($printing_id,$status,$purchase_content = '',$reject_content = ''){

        $PrintingDetail = PrintingModel::find($printing_id);
        if ($PrintingDetail['purchase_status'] != 2){
            return json(['code'=>400,'msg'=>escape('该条已被审批')]);
        }
        if ($status == 1){
            //通过
            $data['purchase_status'] = 1;
            $data['status'] = 3;
            $data['purchase_time'] = date('Y-m-d H:i:s',time());
            $data['purchase_content'] = iconv('utf-8','gbk',$purchase_content);
            $data['purchase_approver'] =  $_SESSION["LOGIN_UID"]; //记录一下
        }else{
            //拒绝
            $data['purchase_status'] = 3;
            $data['status'] = 5;
            $data['purchase_time'] = date('Y-m-d H:i:s',time());
            $data['reject_content'] = iconv('utf-8','gbk',$reject_content);
            $data['purchase_approver'] =  $_SESSION["LOGIN_UID"]; //
        }
        $flag = Db::name('printing')->where('printing_id',$printing_id)->update($data);
        if ($flag){
            return  true;
        }
        return false;
    }
    //总经办审批
    public function GeneralApprove($printing_id,$status,$reject_content){

        $PrintingDetail = PrintingModel::find($printing_id);
        if ($PrintingDetail['general_status'] != 2){
            return json(['code'=>400,'msg'=>escape('该条已被审批')]);
        }

        if ($status == 1){
            //通过
            $data['general_status'] = 1;
            $data['status'] = 4;
            $data['general_time'] = date('Y-m-d H:i:s',time());
            $data['general_approver'] =  $_SESSION["LOGIN_UID"]; //记录一下
        }else{
            //拒绝
            $data['general_status'] = 3;
            $data['status'] = 5;
            $data['general_time'] = date('Y-m-d H:i:s',time());
            $data['reject_content'] = iconv('utf-8','gbk',$reject_content);
            $data['general_approver'] =  $_SESSION["LOGIN_UID"]; //记录一下
        }
        $flag = Db::name('printing')->where('printing_id',$printing_id)->update($data);
        if ($flag){
            return  true;
        }
        return false;
    }

    //采购部审批 并提交内容
    public function InspectorApprove($printing_id,$status,$inspector_content,$reject_content){

        $PrintingDetail = PrintingModel::find($printing_id);
        if ($PrintingDetail['inspector_status'] != 2){
            return json(['code'=>400,'msg'=>escape('该条已被审批')]);
        }

        if ($status == 1){
            //通过
            $data['inspector_status'] = 1;
            $data['status'] = 1; //完成
            $data['inspector_time'] = date('Y-m-d H:i:s',time());
            $data['inspector_content'] = iconv('utf-8','gbk',$inspector_content);
            $data['inspector_approver'] =  $_SESSION["LOGIN_UID"]; //记录一下
        }else{
            //拒绝
            $data['inspector_status'] = 3;
            $data['status'] = 5;
            $data['inspector_time'] = date('Y-m-d H:i:s',time());
            $data['reject_content'] = iconv('utf-8','gbk',$reject_content);
            $data['inspector_approver'] =  $_SESSION["LOGIN_UID"]; //记录一下
        }
        $flag = Db::name('printing')->where('printing_id',$printing_id)->update($data);
        if ($flag){
            return  true;
        }
        return false;
    }

    //获取 全部人员上传的文件
    public function GetPrintingFile($printing_id){
        $PrintingEnclosure = Db::name('printing_enclosure')
            ->where('printing_id',$printing_id)
            ->order('section_status','asc')
            ->select()
            ->toArray();
        $content = '';
        foreach($PrintingEnclosure as $key=>$val){
            $PrintingEnclosure[$key]['user_name'] =  iconv('gbk','utf-8', $val['user_name']);
            $PrintingEnclosure[$key]['create_time'] = date('Y-m-d H:i',$val['create_time']);
            switch ($val['section_status']){
                case $val['section_status'] == 1:
                    $content  .= '<div style="float: left;margin: 1.5rem;border: 1px dashed #000;border-radius: 5px;text-align:center"><span>销售部:</span>'.'<span>'.$PrintingEnclosure[$key]['user_name'].'</span>'.'<div>'.'上传于'.$PrintingEnclosure[$key]['create_time'].'</div>'.'<a style="color: white;padding: 6px 10px 6px 10px;border-radius: 15%;background-color: black;" href='.$val['enclosure_file'].' target="_blank">点击下载</a></div> ';
                    break;
                case $val['section_status'] == 2:
                    $content  .= '<div style="float: left;margin: 1.5rem;border: 1px dashed #000;border-radius: 5px;text-align:center"><span>采购部:</span>'.'<span>'.$PrintingEnclosure[$key]['user_name'].'</span>'.'<div>'.'上传于'.$PrintingEnclosure[$key]['create_time'].'</div>'.'<a style="color: white;padding: 6px 10px 6px 10px;border-radius: 15%;background-color: black;" href='.$val['enclosure_file'].' target="_blank">点击下载</a> </div>';
                    break;
                case $val['section_status'] == 3:
                    $content  .= '<div style="float: left;margin: 1.5rem;border: 1px dashed #000;border-radius: 5px;text-align:center"><span>质检部:</span>'.'<span>'.$PrintingEnclosure[$key]['user_name'].'</span>'.'<div>'.'上传于'.$PrintingEnclosure[$key]['create_time'].'</div>'.'<a style="color: white;padding: 6px 10px 6px 10px;border-radius: 15%;background-color: black;" href='.$val['enclosure_file'].' target="_blank">点击下载</a> </div>';
                    break;
                default:

                    break;
            }
        }
        return $content;

    }


}